home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / dos_win / winsock / maillist / 94-03.Z / 94-03 / text0298.txt < prev    next >
Encoding:
Text File  |  1994-03-30  |  7.0 KB  |  189 lines

  1.  
  2. I have a winsock app that used to work fine, but I upgraded a couple of
  3. stacks and the stuff stopped working.  Since it happened on two different
  4. stacks, I figure the problem must be in my code.  Maybe I was using a
  5. marginal practice that an updated stack would not allow.  Anyway maybe I
  6. can get some help here.
  7.  
  8. The problem has to do with a call to select() on a datagram socket.  I am
  9. implementing a form or RPC (not ONC or DCE or plug in you own acronym) that
  10. sends a packet to a host and then waits up to two seconds for a reply.
  11. Don't do it, I see you reaching for that "Just use the async calls..."
  12. stuff.  I really want to do a synchronous function call across the network.
  13. The call uses select to tell me if any data arrives on the socket.
  14.  
  15. In the previous versions, this worked quite well.  Now the select always
  16. returns immediately that the fd_set is ready for reading.  Of course, I
  17. then read the socket and the recv_from returns 0 bytes.  How can I code
  18. around this?
  19.  
  20. I have tried making the socket blocking and non-blocking and the same
  21. behaviour is displayed.  I was thinking maybe I have to just loop on the
  22. recv_from but then the timing gets tough.  I could make a timer callback
  23. that would set a flag, but that would cost a
  24. makeprocinstance/settimer/killtimer for every single packet.
  25.  
  26. I have the code below that makes these calls.  Please help if you can.  I
  27. will certainly post the resolution if anybody is interested.
  28.  
  29. ------- Begin Code Fragment -------------------
  30.  
  31. // Background stuff.....
  32. this code uses a UDP socket to receive packets from a server.  The packets 
  33. are 608 bytes in size and using a protocol analyzer, I can see them being 
  34. returned to the application.  The problem is that the select call below 
  35. returns 1 immediately (not SOCKET_ERROR or 0) even though the packet has not 
  36. yet arrived.  The recv_from() does not get any data.  We tried all 
  37. combinations of blocking and non-blocking sockets to no avail.
  38.  
  39.  
  40. cm_recv    FAR PASCAL recv_packet( rpc_conv FAR *conv, char FAR *packet, int FAR *size)
  41. {
  42.   /* Function to receive a response from the server    */
  43.  
  44.   struct sockaddr_in    sender;
  45.   int                    len;
  46.   int                    rc;
  47.   fd_set                 fdset;
  48.   struct timeval        timer;
  49.   char                    msg_buf[64];
  50.  
  51.   FD_ZERO(&fdset);
  52.   FD_SET(conv->socknum,&fdset);
  53.   timer.tv_sec = 2;
  54.   timer.tv_usec = 0;
  55.   /* to check if any data is available to be read */
  56.   switch ( select( 0, &fdset, NULL, NULL, &timer) ) {
  57.     case 0:
  58.         return RPC_TIMEOUT;
  59.     case SOCKET_ERROR:
  60.         wsprintf(msg_buf,"Select Error, errno = %d",
  61.                 WSAGetLastError());
  62.         MessageBox ( NULL, msg_buf,"CM_API",
  63.                 MB_ICONASTERISK | MB_OK);
  64.         return RPC_ERROR; /* Some error other than a wouldblock   */
  65.     default:
  66.         // Code always takes this path even if no data on the socket
  67.         rc = recvfrom (conv->socknum,
  68.                     (LPSTR) packet,
  69.                     *size,
  70.                     0, (LPSOCKADDR) &sender, &len);
  71.         *size = rc;
  72.           return RPC_OK;
  73.   }
  74. }
  75.  
  76.  
  77. // Code that creates the socket....
  78.  
  79. cm_bool FAR PASCAL makesocket( rpc_conv FAR *conv, int flag )
  80. {
  81.   /* Create a socket for communication with the API server    */
  82.  
  83.   WSADATA     winconfig;
  84.   int         socket_error;
  85.   struct    hostent FAR *hent;
  86.   char        msg_buf[64];
  87.   int         localport, rc;
  88.  
  89.   if (flag) {
  90.       /* Startup the winsocket DLL */
  91.       if ((socket_error = WSAStartup( 0x0101, (LPWSADATA) &winconfig)) != 0) {
  92.         wsprintf(msg_buf, "WSAStartup Error, errno = %d", socket_error);
  93.         MessageBox ( NULL, msg_buf, "CM_API", MB_ICONASTERISK | MB_OK);
  94.         return CM_FALSE;
  95.       }
  96.   }
  97.  
  98.   /* Find the UDP server host */
  99.   if ( (hent = gethostbyname( conv->server )) == NULL ) {
  100.     wsprintf(msg_buf, "Cannot locate server %s", (LPSTR)conv->server);
  101.     MessageBox ( NULL, msg_buf, "CM_API", MB_ICONASTERISK | MB_OK);
  102.     return CM_FALSE;
  103.   }
  104.   /* Save the server host in the peer field    */
  105.   conv->peer.sin_family=AF_INET;
  106.   conv->peer.sin_addr.s_addr = *( (unsigned long far *) hent->h_addr_list[0]);
  107.   conv->peer.sin_port = htons(CM_RPC_PORT);
  108.  
  109.   /* Create a socket to talk to the server    */
  110.   if ( (conv->socknum = socket( AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET ) {
  111.     wsprintf(msg_buf, "Cannot create socket, errno = %d", WSAGetLastError());
  112.     MessageBox ( NULL, msg_buf, "CM_API", MB_ICONASTERISK | MB_OK);
  113.     return CM_FALSE;
  114.   }
  115.  
  116.   /* Set the socket to blocking */
  117.   u_long     block_flag = 0;
  118.   if ( ioctlsocket( conv->socknum,
  119.                     (long)FIONBIO,
  120.                     (unsigned long FAR *)&block_flag) == SOCKET_ERROR ) {
  121.     wsprintf(msg_buf, "Could not make socket blocking, errno = %d", 
  122.                 WSAGetLastError());
  123.     MessageBox ( NULL, msg_buf, "CM_API", MB_ICONASTERISK | MB_OK);
  124.     return CM_FALSE;
  125.   }
  126.   
  127.  
  128.   _fmemset((void FAR *) &conv->myname, 0x00, sizeof(struct sockaddr_in));
  129.   conv->myname.sin_family=AF_INET;
  130.   conv->myname.sin_addr.s_addr = 0;
  131.  
  132.   for (localport = CM_RPC_PORT; localport < CM_RPC_PORT + MAX_SESSIONS_PC; localport++) {
  133.     conv->myname.sin_port = htons(localport);
  134.     rc = bind ( conv->socknum,
  135.             (struct sockaddr FAR *) &conv->myname,
  136.             sizeof(struct sockaddr_in) );
  137.     if ( rc == 0 ) return CM_TRUE;
  138.     if ( (rc == SOCKET_ERROR) && (WSAGetLastError() != WSAEADDRINUSE) ) {
  139.         wsprintf(msg_buf, "Could not bind name to socket, errno = %d", WSAGetLastError());
  140.         MessageBox ( NULL, msg_buf, "CM_API", MB_ICONASTERISK | MB_OK);
  141.         return CM_FALSE;
  142.     }
  143.   }
  144.   wsprintf(msg_buf, "%s","All Sockets busy, try later.");
  145.   MessageBox ( NULL, msg_buf, "CM_API", MB_ICONASTERISK | MB_OK);
  146.   return CM_FALSE;
  147. }
  148. -- 
  149. **********************************************************************
  150. * Mike Vargo                    | Centigram Communications Corp      *
  151. *                               | 91 East Tasman Dr.                 *
  152. * Internet: mfvargo@netcom.com  | San Jose, CA  95134                *
  153. * Compuserve: 72212,3124        | Telephone: 408-428-3748            *
  154. **********************************************************************
  155. From rcq@mailserv-D.ftp.com Wed Mar 23 07:46:42 1994
  156. Received: from ftp.com (wd40.ftp.com) by SunSITE.Unc.EDU (5.65c+IDA/FvK-1.07) with SMTP
  157.           id AA18411; Wed, 23 Mar 1994 12:47:28 -0500
  158. Received: from mailserv-D.ftp.com by ftp.com  ; Wed, 23 Mar 1994 12:47:23 -0500
  159. Received: from rcq.oysters.ftp.com by mailserv-D.ftp.com (5.0/SMI-SVR4)
  160.     id AA28641; Wed, 23 Mar 94 12:46:42 EST
  161. Date: Wed, 23 Mar 94 12:46:42 EST
  162. Message-Id: <9403231746.AA28641@mailserv-D.ftp.com>
  163. To: dennyp@sunny.bws.com
  164. Subject: Re: re. Winsock Providers
  165. From: rcq@ftp.com  (Bob Quinn)
  166. Reply-To: rcq@ftp.com
  167. Cc: Multiple recipients of list <winsock@sunsite.unc.edu>
  168. Sender: rcq@mailserv-D.ftp.com
  169. Repository: mailserv-D.ftp.com, [message accepted at Wed Mar 23 12:46:32 1994]
  170. Originating-Client: oysters.ftp.com
  171. Content-Length: 584
  172.  
  173. >       There may be others but the people that come to mind as WINSOCK.DLL
  174. >   providers are:
  175. >   NetManage                Chameloen
  176. >   Novell                        Lan WorkPlace
  177. >   Beame & Whiteside  BW-TCP/BW-NFS
  178. >   Wollongong Group    Pathway Access
  179. >   Frontier Technol.       Super TCP/IP
  180.  
  181. There "may" be others?  :)  I guess it's easy to forget the minor
  182. players like Microsoft, Sun Microsystems, FTP Software and IBM,
  183. to name a few.
  184.  
  185. --
  186.  Bob Quinn                                             rcq@ftp.com
  187.  FTP Software, Inc.                                No. Andover, MA
  188.  
  189.